package aspirecn.project3.schedule;

import aspirecn.project3.constant.KafkaTopic;
import aspirecn.project3.producer.KafkaProducerService;
import aspirecn.project3.request.MockData;
import aspirecn.project3.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Random;

/**
 * packageName aspirecn.project3.schedule
 *
 * @author 邵一宸
 * @version JDK 17
 * @className ScheduledTasks
 * @date 2024/7/20
 * @description TODO
 */
@Slf4j
@Component
public class ScheduledTasks {

    @Autowired
    KafkaProducerService kafkaProducerService;

    // 定时任务 每十秒钟生成模拟点击数据进入kafka
    @Scheduled(cron = "0/10 * * * * ? ")
    public void performMockDataCronTask() {
        Random random= new Random();
        long randId= random.nextInt(100)+1; //产生一个[1,100]的随机商品id
        int randPointCounts=random.nextInt(5)+1; //产生一个[1,5]的随机点击数
        MockData mockData=new MockData();
//        mockData.setGoodId(Long.valueOf(1)); // 测试用
        mockData.setGoodId(Long.valueOf(randId));
        mockData.setPointCounts(randPointCounts);
//        long currentTime=System.currentTimeMillis();
//        Timestamp timestamp=new Timestamp(currentTime);
//        mockData.setTimestamp(timestamp);
        kafkaProducerService.sendMessage(KafkaTopic.mockDataTopic,JsonUtil.obj2Json(mockData));
        log.info("Cron task executed at:{},发送的消息为:{} ", System.currentTimeMillis(),mockData);
    }
}